package cmd

import (
	"encoding/json"
	"fmt"
	"github.com/spf13/cobra"
	global2 "longmen/server/config/global"
	"longmen/server/internal/services/nami/config"
	"longmen/server/internal/services/nami/services/sports/basketball/bbcache"
	"longmen/server/internal/services/nami/services/sports/football/fbcache"
	"longmen/server/internal/services/nami/utils"
	"longmen/server/pkg/common/lib/bytesconv"
	"longmen/server/pkg/db/mysql/models/models_sports"
)

var syncScoreCli = &cobra.Command{
	Use:   "sync_score",
	Short: "同步比分",
	Long:  "同步比分",
	Run: func(cmd *cobra.Command, args []string) {
		toInt := func(i float64) int {
			return int(i)
		}

		fb := make([]models_sports.FootballMatch, 0)
		global2.Sport().Find(&fb)
		scores := make([]fbcache.Score, 0)
		for _, match := range fb {
			homeScore := make([]float64, 0)
			err := json.Unmarshal(bytesconv.StringToBytes(match.HomeScores), &homeScore)
			if err != nil {
				continue
			}

			awayScore := make([]float64, 0)
			err = json.Unmarshal(bytesconv.StringToBytes(match.AwayScores), &awayScore)
			if err != nil {
				continue
			}
			scores = append(scores, fbcache.Score{
				MatchId:     match.ID,
				MatchStatus: int(match.StatusID),
				HomeScore: fbcache.ScoreItem{
					Score:        toInt(homeScore[0]),
					HalfScore:    toInt(homeScore[1]),
					RedCard:      toInt(homeScore[2]),
					YellowCard:   toInt(homeScore[3]),
					CornerKick:   toInt(homeScore[4]),
					OTScore:      toInt(homeScore[5]),
					PenaltyScore: toInt(homeScore[6]),
				},
				AwayScore: fbcache.ScoreItem{
					Score:        toInt(awayScore[0]),
					HalfScore:    toInt(awayScore[1]),
					RedCard:      toInt(awayScore[2]),
					YellowCard:   toInt(awayScore[3]),
					CornerKick:   toInt(awayScore[4]),
					OTScore:      toInt(awayScore[5]),
					PenaltyScore: toInt(awayScore[6]),
				},
				KickOutTime: 0,
				TimePlayed:  0,
			})
		}
		fmt.Println("fbcount: ", len(scores))
		fbcache.NewScoreRedis().LiveScore(scores)

		bb := make([]models_sports.BasketballMatch, 0)
		global2.Sport().Find(&bb)
		bbScores := make([]bbcache.Score, 0)
		for _, match := range bb {
			hsc := make([]float64, 0)
			err := json.Unmarshal(bytesconv.StringToBytes(match.HomeScores), &hsc)
			if err != nil {
				continue
			}

			asc := make([]float64, 0)
			err = json.Unmarshal(bytesconv.StringToBytes(match.AwayScores), &asc)
			if err != nil {
				continue
			}

			floatToInt := func(ss []float64) []int {
				var ii []int
				for _, val := range ss {
					ii = append(ii, int(val))
				}
				return ii
			}

			homeTotalScore := utils.SumFloat(hsc)
			awayTotalScore := utils.SumFloat(asc)
			bbScores = append(bbScores, bbcache.Score{
				MatchId:        match.ID,
				MatchStatus:    int(match.StatusID),
				TimeLeft:       int(match.TimePlayed),
				AwayScore:      floatToInt(asc),
				HomeScore:      floatToInt(hsc),
				AwayTotalScore: awayTotalScore,
				HomeTotalScore: homeTotalScore,
			})
		}
		bbcache.NewScoreCache().LiveScore(bbScores)
		fmt.Println("bbcount: ", len(bbScores))
	},
	PreRun: func(cmd *cobra.Command, args []string) {
		global2.Initialize(configFile)
		config.InitConfig()
		err := global2.InitElasticSearch()
		if err != nil {
			global2.Log.Fatalf("%s", err.Error())
		}
	},
	PostRun: func(cmd *cobra.Command, args []string) {

	},
}
